/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.easyedu.dictionary.api.options;

import eu.easyedu.dictionary.api.model.Dictionary;
import eu.easyedu.dictionary.api.model.Language;
import eu.easyedu.dictionary.api.util.DictionaryManager;
import java.util.prefs.Preferences;

/**
 *
 * @author hlavki
 */
public class DictionaryOptions extends AbstractOptions {

    private static final String CONNECTION_NAME_PROPERTY = "jdbc.connection.name";
    public static final String PREFERRED_DICTIONARY_PROPERTY = "default.dictionary";
    private static final Language UNKNOWN_LANGUAGE = new Language(-1L);
    private String connectionName;
    private Dictionary defaultDictionary;
    private static final DictionaryOptions instance = new DictionaryOptions();

    protected DictionaryOptions() {
        nodeName = "dictionary";
        Preferences prefs = getPreferences();
        connectionName = prefs.get(CONNECTION_NAME_PROPERTY, null);
        defaultDictionary = DictionaryManager.getDefault().getDictionary(prefs.get(PREFERRED_DICTIONARY_PROPERTY, null));
    }

    public static final DictionaryOptions getInstance() {
        return instance;
    }

    public void setConnectionName(String connectionName) {
        String oldValue = this.connectionName;
        this.connectionName = connectionName;
        firePropertyChange(CONNECTION_NAME_PROPERTY, oldValue, connectionName);
        getPreferences().put(CONNECTION_NAME_PROPERTY, this.connectionName);
    }

    public Dictionary getDefaultDictionary() {
        return defaultDictionary;
    }

    public void setDefaultDictionary(Dictionary defaultDictionary) {
        Dictionary oldValue = this.defaultDictionary;
        this.defaultDictionary = defaultDictionary;
        firePropertyChange(PREFERRED_DICTIONARY_PROPERTY, oldValue, defaultDictionary);
        getPreferences().put(PREFERRED_DICTIONARY_PROPERTY,
                this.defaultDictionary != null ? this.defaultDictionary.getConnectionName() : "");
    }
}
